import pandas as pd
import numpy as np
from typing import Optional, Tuple

def combine_weights(
    weight1: pd.Series,
    weight2: pd.Series,
    alpha: float = 0.5,
    normalize: bool = True,
    validate: bool = True
) -> pd.Series:
    """
    Combine two权重 series using alpha-weighted combination
    
    Parameters:
        weight1 (pd.Series): First权重 series (e.g., entropy weights)
        weight2 (pd.Series): Second权重 series (e.g., independence weights)
        alpha (float): Weighting factor for first series (0-1)
        normalize (bool): Whether to normalize the combined weights
        validate (bool): Whether to validate input data
    
    Returns:
        pd.Series: Combined and optionally normalized weights
        
    Raises:
        ValueError: If inputs are invalid or combinations are negative
    """
    # Validate input if required
    if validate:
        if not isinstance(weight1, pd.Series) or not isinstance(weight2, pd.Series):
            raise TypeError("Both inputs must be pd.Series objects")
        if weight1.index._typ == weight2.index._typ == 'unknown':
            raise ValueError("Both series must have the same index")
        if weight1.shape != weight2.shape:
            raise ValueError("Both series must have the same length")
        
        # Validate numeric values
        if not (weight1.dtype.kind in 'fi' and weight2.dtype.kind in 'fi'):
            raise TypeError("Both series must contain numeric values")
        if any(weights < 0 for weights in [weight1, weight2]):
            raise ValueError("All weights must be non-negative")
        
        # Allowable alpha range
        if alpha < 0 or alpha > 1:
            raise ValueError(f"Alpha value ({alpha}) must be between 0 and 1")
    
    # Perform the combination
    a = np.array(weight1)
    b = np.array(weight2)
    combined = alpha * a + (1 - alpha) * b
    
    # Handle edge cases
    if normalize:
        total = combined.sum()
        if abs(total) < 1e-10:
            raise ValueError("Combined weights sum to zero - normalization impossible")
        
        combined_normalized = combined / total
        # Check for negative weights after normalization
        if any(combined_normalized < 0 - 1e-10):
            raise ValueError("Some combined weights are negative")
        
        return pd.Series(combined_normalized, index=weight1.index)
    
    return pd.Series(combined, index=weight1.index)
